<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Favorite;
use App\Model\M9_good;

class FavoriteController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex()
    {
        //
        $uid = session('user')->uid;
        $favorite = Favorite::where('f_uid',$uid)->get();
        $good=[];
        foreach ($favorite as $key => $value) {
            $gid = ($value->f_gid);
            $good[] = M9_good::find($gid);
        }
        return view('home.favorite',['title'=>'我的收藏','good'=>$good]);
    }

    
    public function getInsert($gid)
    {
        //
        $uid = session('user')->uid;
        $favorite = new Favorite;
        $favorite->f_uid = $uid;
        $favorite->f_gid = $gid;
        if($favorite->save()){
            return back()->with('success','收藏成功');
        }else{
            return back()->with('error','收藏失败');
        }
    }

    
    public function getDestroy(Request $request)
    {
        //
        $gid = $request->only('gid');
        if(Favorite::where('f_gid',$gid)->delete()){
            return back()->with('success','删除成功');
        }else{
            return back()->with('error','删除失败');
        }
    }
}
